﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.DataLayer;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.member;

namespace Ulriksen.Umbraco.OpenId
{
    public class OpenIdMember
    {
        public static umbraco.cms.businesslogic.member.Member GetMember(string claimedIdentifier)
        {
            int memberNodeId = Application.SqlHelper.ExecuteScalar<Int32>("select nodeId from nuMemberOpenid where OpenId = @openId",
                Application.SqlHelper.CreateParameter("@openId", claimedIdentifier));

            if (memberNodeId == 0)
                return null;
            else
                return new umbraco.cms.businesslogic.member.Member(memberNodeId);
        }

        public static void RemoveOpenId(string openIdIdentifier)
        {
            Application.SqlHelper.ExecuteNonQuery("delete from nuMemberOpenid where OpenId = @OpenId",
               Application.SqlHelper.CreateParameter("@OpenId", openIdIdentifier));
        }

        public static string OpenIdToUserName(string openId)
        {
            return openId.Replace("http://", "");
        }
    }

}
